import 'package:boss/textStyle/text_style.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:boss/ui/widget/radioList/radio.dart' as radio;

class RowRadio<T> extends StatelessWidget {
  final List<RowRadioData<T>> items;
  final T value;
  final ValueChanged<T> onChanged;
  final EdgeInsets padding;

  const RowRadio({
    Key key,
    @required this.items,
    this.value,
    this.onChanged,
    this.padding,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: this.padding,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: items
            .map((item) => RowRadioItem<T>(
                  data: item,
                  isSelected: item.value == value,
                  onTap: () {
                    if (onChanged != null) {
                      onChanged(item.value);
                    }
                  },
                ))
            .toList(),
      ),
    );
  }
}

class RowRadioItem<T> extends StatelessWidget {
  final RowRadioData<T> data;
  final bool isSelected;
  final VoidCallback onTap;

  const RowRadioItem({
    Key key,
    this.data,
    this.isSelected = false,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      behavior: HitTestBehavior.translucent,
      child: Row(
        children: <Widget>[
          radio.Radio(
            isSelected: isSelected,
          ),
          Container(
            margin: EdgeInsets.only(
              left: 10.w,
            ),
            child: Text(
              data.title,
              style: TextStyleConstant().normal_1_28,
            ),
          ),
        ],
      ),
    );
  }
}

class RowRadioData<T> {
  String title;
  T value;

  RowRadioData({this.title, this.value});
}
